﻿
using System;
using TripleA.Core.Interfaces;
using TripleA.Core.Pipeline;
using FluentAssertions;

namespace TripleA.StoryQ
{
    public abstract class StoryQTestDomain<T> : BddTestDomain
    {
        private IPipelineConfig<T> _stepRunner;
        protected T Context;

        /// <summary>
        /// Override this if you need to construct your context with ctor args
        /// </summary>
        /// <returns></returns>
        protected virtual T GetContext()
        {
            return Activator.CreateInstance<T>();
        }

        protected IPipelineConfig<T> TripleA
        {
            get { return _stepRunner ?? (_stepRunner = GetStepRunner()); }
        }

        protected virtual IPipelineConfig<T> GetStepRunner()
        {
            Context = GetContext();
            return new BddPipelineConfig<T>(Context);
        }

        public void TheTypeForException_Is_(int index, Type type)
        {
            _expectedException.Should().NotBeNull();
            _expectedException.Should().BeOfType<AggregateException>();

            var aggEx = (AggregateException) _expectedException;

            aggEx.InnerExceptions.Count.Should().BeGreaterThan(index);
            aggEx.InnerExceptions[index].GetType().Should().Be(type);
        }

        public void TheMessageForException_Contains_(int index, string text)
        {
            _expectedException.Should().NotBeNull();
            _expectedException.Should().BeOfType<AggregateException>();

            var aggEx = (AggregateException) _expectedException;

            aggEx.InnerExceptions.Count.Should().BeGreaterThan(index);
            aggEx.InnerExceptions[index].Message.Contains(text).Should().BeTrue();
        }

    }
}